﻿(function (Ctn){
Ctn.prototype.extend({
    //添加监听
    addListener: function(evt, callback, data){
        return data == null ? this.bind(evt, callback) : this.bind(evt, data, callback);
    },
    //删除监听
    removeListener: function(evt, callback){
        return this.unbind(evt, callback);
    }
});

Ctn.extend(Ctn.event, {
    KEY_BACKSPACE: 8,
    KEY_TAB:       9,
    KEY_ENTER:    13,
    KEY_ESC:      27,
    KEY_LEFT:     37,
    KEY_UP:       38,
    KEY_RIGHT:    39,
    KEY_DOWN:     40,
    KEY_DELETE:   46,
    KEY_HOME:     36,
    KEY_END:      35,
    KEY_PAGEUP:   33,
    KEY_PAGEDOWN: 34,
    from: function(evt){
        evt = evt || window.event;
        return {
            data: evt.data,
            //事件触发点元素
            element: evt.target || evt.srcElement,
            //按下鼠标左键
            isLeftClick: (((evt.which) && (evt.which == 1)) ||
                ((evt.button) && (evt.button == 1))),
            //按下鼠标右键
            isRightClick: (((evt.which) && (evt.which == 2)) ||
                ((evt.button) && (evt.button == 2))),
            //是否按下回车键
            isEnterPress: evt.keyCode == Ctn.event.KEY_ENTER,
            //事件触发点X轴
            pointerX: evt.pageX || (evt.clientX +
                (document.documentElement.scrollLeft || document.body.scrollLeft)),
            //事件触发点Y轴
            pointerY: evt.pageY || (evt.clientY +
                (document.documentElement.scrollTop || document.body.scrollTop)),
            //停止默认操作
            stop: function() {
                if (evt.preventDefault) {
                  evt.preventDefault();
                  evt.stopPropagation();
                } else {
                  evt.returnValue = false;
                  evt.cancelBubble = true;
                }
            },
            //根据标签向上查找元素
            findElement: function(tagName) {
                var element = Ctn.event.element(evt);
                while (element.parentNode && (!element.tagName ||
                    (element.tagName.toUpperCase() != tagName.toUpperCase())))
                  element = element.parentNode;
                return element;
            }
        };
    }
});
})(Ctn)